package com.atmc.service.impl;

import com.atmc.entity.DetailsOrder;
import com.atmc.entity.GoodsOrder;
import com.atmc.entity.OrderRequest;
import com.atmc.entity.ResponseResult;
import com.atmc.mapper.DetailsOrderMapper;
import com.atmc.mapper.GoodsOrderMapper;
import com.atmc.service.GoodsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: SpringCloud1216
 * @BelongsPackage: com.atmc.service.impl
 * @Author: chuyings
 * @CreateTime: 2022-11-17  15:06
 * @Version: 1.0
 */
@Service
public class GoodsOrderServiceImpl implements GoodsOrderService {
    @Autowired
    GoodsOrderMapper goodsOrderMapper;
    @Autowired
    DetailsOrderMapper detailsOrderMapper;
    @Override
    public List<GoodsOrder> queryGoodsOrderAll() {
        return goodsOrderMapper.queryGoodsOrderAll();
    }

    @Override
    public int insertOrderGoodsOne(GoodsOrder goodsOrder) {
        return goodsOrderMapper.insertOrderGoodsOne(goodsOrder);
    }

    @Override
    public ResponseResult addGoodsOrder(OrderRequest orderRequest) {
        Date date=new Date();
        //提交总价 用户名 订单编号到DetailsOrder表
        DetailsOrder detailsOrder=new DetailsOrder();
        detailsOrder.setAllPrice(orderRequest.getAllPrice());
        detailsOrder.setDetailsNumber(date.getTime());
        detailsOrder.setUserName(orderRequest.getUserName());
        detailsOrderMapper.insertDetailsOrder(detailsOrder);

        List<GoodsOrder> goodsOrders = orderRequest.getGoodsOrders();

//        GoodsOrder goodsOrder;
        for (GoodsOrder order : goodsOrders) {
            GoodsOrder goodsOrder=new GoodsOrder();
            goodsOrder.setUserName(orderRequest.getUserName());
            goodsOrder.setOrderNumber(date.getTime());
            goodsOrder.setGoodsName(order.getGoodsName());
            goodsOrder.setSingPrice(order.getSingPrice());
            goodsOrder.setGoodsNum(order.getGoodsNum());
            goodsOrderMapper.insertOrderGoodsOne(goodsOrder);
        }
        return new ResponseResult(200,"提交订单成功");
    }
}
